package com.farm.project.service.impl;

import com.farm.project.domain.Tasklink;
import com.farm.project.domain.Tasknode;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;

import com.farm.project.dao.TasklinkDaoInter;
import com.farm.project.dao.TasknodeDaoInter;
import com.farm.project.service.TasklinkServiceInter;
import com.farm.project.service.TasknodeServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：任务节点服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class TasknodeServiceImpl implements TasknodeServiceInter {
	@Resource
	private TasknodeDaoInter tasknodeDaoImpl;
	@Resource
	private TasklinkDaoInter tasklinkDaoImpl;
	@Resource
	private TasklinkServiceInter taskLinkServiceImpl;
	private static final Logger log = Logger.getLogger(TasknodeServiceImpl.class);

	@Override
	@Transactional
	public Tasknode insertTasknodeEntity(Tasknode entity, LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setEuser(user.getId());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		return tasknodeDaoImpl.insertEntity(entity);
	}

	@Override
	@Transactional
	public Tasknode editTasknodeEntity(Tasknode entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		Tasknode entity2 = tasknodeDaoImpl.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setProjectid(entity.getProjectid());
		entity2.setRid(entity.getRid());
		entity2.setModel(entity.getModel());
		entity2.setPcontent(entity.getPcontent());
		entity2.setPstate(entity.getPstate());
		entity2.setEuser(entity.getEuser());
		entity2.setCtime(entity.getCtime());
		entity2.setCuser(entity.getCuser());
		entity2.setEtime(entity.getEtime());
		entity2.setId(entity.getId());
		tasknodeDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteTasknodeEntity(String id, LoginUser user) {
		Tasknode node = tasknodeDaoImpl.getEntity(id);
		tasknodeDaoImpl.deleteEntity(tasknodeDaoImpl.getEntity(id));
		{
			List<Tasklink> link1 = tasklinkDaoImpl
					.selectEntitys(DBRuleList.getInstance().add(new DBRule("PROJECTID", node.getProjectid(), "="))
							.add(new DBRule("TNODEID", id, "=")).toList());
			Tasklink dellink1 = link1.get(0);
			for (Tasklink linknode : link1) {
				tasklinkDaoImpl.deleteEntity(linknode);
			}
			List<Tasklink> link2 = tasklinkDaoImpl
					.selectEntitys(DBRuleList.getInstance().add(new DBRule("PROJECTID", node.getProjectid(), "="))
							.add(new DBRule("SNODEID", id, "=")).toList());
			Tasklink dellink2 = link2.get(0);
			for (Tasklink linknode : link2) {
				tasklinkDaoImpl.deleteEntity(linknode);
			}
			{// 补齐后续连线
				List<Tasklink> startNodeSubs = tasklinkDaoImpl
						.selectEntitys(DBRuleList.getInstance().add(new DBRule("PROJECTID", node.getProjectid(), "="))
								.add(new DBRule("SNODEID", dellink1.getSnodeid(), "=")).toList());
				if (!dellink2.getTnodeid().equals("END") || startNodeSubs.size() <= 0) {
					Tasklink linkNew = new Tasklink();
					linkNew.setSnodeid(dellink1.getSnodeid());
					linkNew.setTnodeid(dellink2.getTnodeid());
					linkNew.setProjectid(dellink1.getProjectid());
					taskLinkServiceImpl.insertTasklinkEntity(linkNew, user);
				}
			}
		}
	}

	@Override
	@Transactional
	public Tasknode getTasknodeEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return tasknodeDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createTasknodeSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "CBTG_PROJECT_TASKNODE",
				"ID,PROJECTID,RID,MODEL,PCONTENT,PSTATE,EUSER,CTIME,CUSER,ETIME");
		return dbQuery;
	}

}
